<?php


namespace app\admin\model;


use think\Model;

class StaffChatTime extends Model
{
    protected $autoWriteTimestamp = true;

    public $create_day;
    public $staff_no;
    public $user_no;
    public $time_long;

    /**
     * @param $staffNo
     * @param $userNo
     * @param $deviceType
     * @return string
     * 对话时间累加
     */
    public static function addChatTime($staffNo,$userNo,$deviceType):string
    {
        $createDay = strtotime(date("Y-m-d",time()));
        $ti = StaffChatTime::where("create_day",$createDay)
            ->where("staff_no",$staffNo)->where("user_no",$userNo)
            ->where("device_type",$deviceType)
            ->find();
        if($ti){
            // 统计时间
            $resTime = timeDiff(strtotime($ti["create_time"] ),strtotime($ti["update_time"]));
            $arr = ["time_long"=>$resTime["min"],"user_no"=>$userNo,"update_time"=>time()];
            StaffChatTime::where("id",$ti["id"])->data($arr)->update();
        }else{
            $timeArr = [
                "create_day" => $createDay,
                "user_no"=>$userNo,
                "staff_no"=>$staffNo,
                "device_type"=>$deviceType
            ];
            StaffChatTime::create($timeArr);
        }
        return "";
    }
}
